﻿Imports System.IO

' Author: Ben Glasser WP3
' Date: 11-15-11
' File: GameWindow.vb
' Purpose: creates main user interface


Public Class GameWindow
    Inherits System.Windows.Forms.Form
    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(GameWindow))
#Region "API Declarations"
    Private Declare Sub ReleaseCapture Lib "user32" ()
    Private Declare Sub SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
    hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
    ByVal lParam As Integer)

    Private Const WM_NCLBUTTONDOWN As Integer = &HA1
    Private Const HTCAPTION As Integer = 2
#End Region


    ' enumerate types of land for readability
    Enum SPRITES_
        MEADOW = 0
        FOREST = 1
        WATER = 2
        WALL = 3
        AXE = 4
        BINOCULAR = 5
        BLACKBERRY_BUSHES = 6
        BOULDER = 7
        CHAINSAW = 8
        CHISEL = 9
        HATCHET = 10
        JACKHAMMER = 11
        MACHETE = 12
        POWERBAR = 13
        ROYAL_DIAMONDS = 14
        SLEDGE = 15
        TREASURE_CHEST = 16
        TREE = 17
        HERO = 18
        NONE = 19
        SHEARS = 20
        BOAT = 21
    End Enum
    ' determine enumerated values by string
    Private Function contentEnum(ByVal content As String)
        Select Case (content.ToLower)
            Case "axe"
                Return SPRITES_.AXE
            Case "binoculars"
                Return SPRITES_.BINOCULAR
            Case "blackberry bushes"
                Return SPRITES_.BLACKBERRY_BUSHES
            Case "boulder"
                Return SPRITES_.BOULDER
            Case "chainsaw"
                Return SPRITES_.CHAINSAW
            Case "chisel"
                Return SPRITES_.CHISEL
            Case "hatchet"
                Return SPRITES_.HATCHET
            Case "jackhammer"
                Return SPRITES_.JACKHAMMER
            Case "machete"
                Return SPRITES_.MACHETE
            Case "power bar"
                Return SPRITES_.POWERBAR
            Case "royal diamonds"
                Return SPRITES_.ROYAL_DIAMONDS
            Case "sledge"
                Return SPRITES_.SLEDGE
            Case "treasure chest"
                Return SPRITES_.TREASURE_CHEST
            Case "type 1 treasure chest"
                Return SPRITES_.TREASURE_CHEST
            Case "type 2 treasure chest"
                Return SPRITES_.TREASURE_CHEST
            Case "tree"
                Return SPRITES_.TREE
            Case "none"
                Return SPRITES_.NONE
            Case "shears"
                Return SPRITES_.SHEARS
            Case "boat"
                Return SPRITES_.BOAT
        End Select

        Return SPRITES_.NONE
    End Function

    ' game variables.
    Dim GameMap As Map = New Map()
    Dim loc As Point = New Point(0, 0)
    Dim WindowLocation As Point = New Point(0, 0)
    Dim LoadHero As Boolean = 0
    Dim mapFile As String

    ' GamePanel_Paint paints the board to GamePanel
    Private Sub GamePanel_Paint() Handles GamePanel.Paint
        Dim location As Point = New Point(0, 0)
        For i As Integer = WindowLocation.Y To (WindowLocation.Y + 9)
            location.Y = i
            For j As Integer = WindowLocation.X To (WindowLocation.X + 9)
                location.X = j
                drawCell(GamePanel.CreateGraphics, location)
            Next
        Next
        If LoadHero Then
            drawHero(GamePanel.CreateGraphics, GameMap.GetHeroLocation())
        End If
        EnergyAmt.Text = GameMap.GetEnergy
        WhiffleAmt.Text = GameMap.GetWhiffles
    End Sub

    ' draws a specified cell to the the given graphics
    Private Sub drawCell(ByVal g As Graphics, ByVal loc As Point)
        ' first draw terrain
        If (Not GameMap.cellIsVisible(loc)) Then
            Sprites.Draw(g, setPos(loc), SPRITES_.NONE)
        Else
            Sprites.Draw(g, setPos(loc), GameMap.getCellTerrain(loc))
            ' then draw content
            If (Not (GameMap.getCellContent(loc).ToLower.Equals("none"))) Then
                Dim str As String = GameMap.getCellContent(loc)
                Dim eq As Boolean = GameMap.getCellContent(loc).ToLower.Equals("none")
                Sprites.Draw(g, setPos(loc), contentEnum(GameMap.getCellContent(loc)))
            End If
        End If
    End Sub

    ' this function tranaslates locations on the gameboard into positions on the screen
    Private Function setPos(ByVal loc As Point) As Point
        Dim position As Point = New Point
        position.X = (loc.X - WindowLocation.X) * Sprites.ImageSize.Width
        position.Y = (loc.Y - WindowLocation.Y) * Sprites.ImageSize.Height
        Return position
    End Function

    ' from toolbar selection open file
    Private Sub OpenMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenMenu.Click
        Dim didWork As Integer

        OpenFile.Filter = "Map(*.map)|*.map|Text(*.txt)|*.txt|All Files(*.*)|*.*"
        didWork = OpenFile.ShowDialog()

        If Not didWork = Windows.Forms.DialogResult.Cancel Then
            mapFile = OpenFile.FileName
            GameMap = New Map(mapFile)
        End If
        WindowLocation = New Point(0, 0)
        LoadHero = True
        GamePanel.Invalidate()
        GamePanel_Paint()
    End Sub

    ' Draw hero at specified coords
    Private Sub drawHero(ByVal g As Graphics, ByVal loc As Point)
        Sprites.Draw(g, setPos(loc), SPRITES_.HERO)
    End Sub

    'Map viewing window directional buttons
    Private Sub upButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upButton.Click
        If WindowLocation.Y > 0 Then
            WindowLocation.Y -= 1
            GamePanel.Invalidate()
            GamePanel_Paint()
        End If
    End Sub

    Private Sub DownButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownButton.Click
        If WindowLocation.Y < (GameMap.getDimension - 9) Then
            WindowLocation.Y += 1
            GamePanel.Invalidate()
            GamePanel_Paint()
        End If
    End Sub

    Private Sub LeftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftButton.Click
        If WindowLocation.X > 0 Then
            WindowLocation.X -= 1
            GamePanel.Invalidate()
            GamePanel_Paint()
        End If
    End Sub

    Private Sub RightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightButton.Click
        If WindowLocation.X < (GameMap.getDimension - 9) Then
            WindowLocation.X += 1
            GamePanel.Invalidate()
            GamePanel_Paint()
        End If
    End Sub

    Private Sub Hero_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        Select Case e.KeyChar
            ' move right
            Case ChrW(Keys.D)
                GameMap.MoveHeroHorizontal(1)
            Case ChrW(Keys.Right)
                GameMap.MoveHeroHorizontal(1)
                ' move left
            Case ChrW(Keys.A)
                GameMap.MoveHeroHorizontal(-1)
            Case ChrW(Keys.Left)
                GameMap.MoveHeroHorizontal(-1)
                ' move up
            Case ChrW(Keys.W)
                GameMap.MoveHeroVertical(-1)
            Case ChrW(Keys.Up)
                GameMap.MoveHeroVertical(-1)
                ' move Down
            Case ChrW(Keys.S)
                GameMap.MoveHeroVertical(1)
            Case ChrW(Keys.Down)
                GameMap.MoveHeroVertical(1)
        End Select
        GamePanel.Invalidate()
        GamePanel_Paint()
    End Sub

    Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
        Me.Close()
    End Sub

    Private Sub Menu_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MenuStrip.MouseDown
        If e.Button = MouseButtons.Left Then
            ReleaseCapture()
            SendMessage(Me.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
        End If
    End Sub
    ' left arrow functions 
    Private Sub leftArrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftArrow.Click
        GameMap.MoveHeroHorizontal(-1)
        GamePanel.Invalidate()
        GamePanel_Paint()
    End Sub
    Private Sub leftArrow_Hover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftArrow.MouseHover
        leftArrow.BackgroundImage = CType(resources.GetObject("leftHover"), System.Drawing.Image)
    End Sub
    Private Sub leftArrow_Out(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftArrow.MouseLeave
        leftArrow.BackgroundImage = CType(resources.GetObject("leftArrow.BackgroundImage"), System.Drawing.Image)
    End Sub
    Private Sub leftArrow_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftArrow.MouseDown
        leftArrow.BackgroundImage = CType(resources.GetObject("leftClick"), System.Drawing.Image)
    End Sub
    Private Sub leftArrow_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftArrow.MouseUp
        leftArrow.BackgroundImage = CType(resources.GetObject("leftHover"), System.Drawing.Image)
    End Sub
    ' right arrow functions
    Private Sub rightArrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightArrow.Click
        GameMap.MoveHeroHorizontal(1)
        GamePanel.Invalidate()
        GamePanel_Paint()
    End Sub
    Private Sub rightArrow_Hover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightArrow.MouseHover
        rightArrow.BackgroundImage = CType(resources.GetObject("rightHover"), System.Drawing.Image)
    End Sub
    Private Sub rightArrow_Out(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightArrow.MouseLeave
        rightArrow.BackgroundImage = CType(resources.GetObject("rightArrow.BackgroundImage"), System.Drawing.Image)
    End Sub
    Private Sub rightArrow_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightArrow.MouseDown
        rightArrow.BackgroundImage = CType(resources.GetObject("rightClick"), System.Drawing.Image)
    End Sub
    Private Sub rightArrow_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightArrow.MouseUp
        rightArrow.BackgroundImage = CType(resources.GetObject("rightHover"), System.Drawing.Image)
    End Sub
    'up Arrow functions
    Private Sub upArrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upArrow.Click
        GameMap.MoveHeroVertical(-1)
        GamePanel.Invalidate()
        GamePanel_Paint()
    End Sub
    Private Sub upArrow_Hover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upArrow.MouseHover
        upArrow.BackgroundImage = CType(resources.GetObject("upHover"), System.Drawing.Image)
    End Sub
    Private Sub upArrow_Out(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upArrow.MouseLeave
        upArrow.BackgroundImage = CType(resources.GetObject("upArrow.BackgroundImage"), System.Drawing.Image)
    End Sub
    Private Sub upArrow_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upArrow.MouseDown
        upArrow.BackgroundImage = CType(resources.GetObject("upClick"), System.Drawing.Image)
    End Sub
    Private Sub upArrow_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upArrow.MouseUp
        upArrow.BackgroundImage = CType(resources.GetObject("upHover"), System.Drawing.Image)
    End Sub
    'down arrow functions
    Private Sub downArrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downArrow.Click
        GameMap.MoveHeroVertical(1)
        GamePanel.Invalidate()
        GamePanel_Paint()
    End Sub
    Private Sub downArrow_Hover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downArrow.MouseHover
        downArrow.BackgroundImage = CType(resources.GetObject("downHover"), System.Drawing.Image)
    End Sub
    Private Sub downArrow_Out(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downArrow.MouseLeave
        downArrow.BackgroundImage = CType(resources.GetObject("downArrow.BackgroundImage"), System.Drawing.Image)
    End Sub
    Private Sub downArrow_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downArrow.MouseDown
        downArrow.BackgroundImage = CType(resources.GetObject("downClick"), System.Drawing.Image)
    End Sub
    Private Sub downArrow_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downArrow.MouseUp
        downArrow.BackgroundImage = CType(resources.GetObject("downHover"), System.Drawing.Image)
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim myStream As Stream
        Dim myString As String = GameMap.ToString()
        Dim buff As New System.Text.UTF8Encoding

        SaveGame.Filter = "txt files (*.txt)|*.txt|Map files (*.map)|*.map"
        SaveGame.FilterIndex = 2
        SaveGame.RestoreDirectory = True

        If SaveGame.ShowDialog() = DialogResult.OK Then
            myStream = SaveGame.OpenFile()
            If (myStream IsNot Nothing) Then
                myStream.Write(buff.GetBytes(myString), 0, buff.GetBytes(myString).Count)
                GameMap.Save(SaveGame.FileName)
                myStream.Close()
            End If
        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        GameMap.Save(mapFile)
    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        GameMap.Random("Random Map", 10, 25)
        LoadHero = True
        GamePanel_Paint()
    End Sub

    Private Sub GameWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
